package service;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import item.Mail;
import item.sqliteConnection;

public class mailService {

    private sqliteConnection sqliteCon;
    public mailService(Context context){
        sqliteCon = new sqliteConnection(context);
    }
    public void insert(Mail mail){
        System.out.println(mail.getName()+" "+mail.getNumber());
        SQLiteDatabase db=sqliteCon.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",mail.getName());
        values.put("number",mail.getNumber());
        values.put("class",mail.getGrade());
        values.put("gender",mail.getGender());
        values.put("email",mail.getEmail());
        long id=db.insert("mail",null,values);
        mail.setId(id);
        db.close();
    }

    public void delete(long id){
        SQLiteDatabase db = sqliteCon.getWritableDatabase();
        db.delete("mail","_id=?",new String[]{id+""});
        db.close();
    }

    public void update(Mail mail){
        SQLiteDatabase db = sqliteCon.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",mail.getName());
        values.put("number",mail.getNumber());
        values.put("class",mail.getGrade());
        values.put("gender",mail.getGender());
        values.put("email",mail.getEmail());
         db.update("mail",values,"_id=?",new String[]{mail.getId()+"" });
         System.out.println("修改成功");
        db.close();
    }

    public Mail query(long id){
        SQLiteDatabase db = sqliteCon.getReadableDatabase();
        Cursor c = db.query("mail",null,"_id=?",new String[]{String.valueOf(id)},null,null
                ,null, String.valueOf(1));
        c.moveToNext();
        System.out.println(c);
        Mail m = new Mail();
//        m.setId(c.getLong(c.getColumnIndex("_id")));
        m.setName(c.getString(c.getColumnIndex("name")));
        m.setNumber(c.getString(c.getColumnIndex("number")));
        m.setEmail(c.getString(c.getColumnIndex("email")));
        m.setGender(c.getString(c.getColumnIndex("gender")));
        m.setGrade(c.getString(c.getColumnIndex("class")));
        c.close();
        db.close();
        return m;
    }

    public List<Mail> search(String i){
        SQLiteDatabase db = sqliteCon.getReadableDatabase();
        Cursor c;

        List<Mail> list = new ArrayList<>();
        System.out.println("search:name");
        c  = db.query("mail",null,"name=?",new String[]{String.valueOf(i)},null,null
                ,"_id DESC");
        if(c.getCount() == 0){
            System.out.println("search:class");
            c  = db.query("mail",null,"class=?",new String[]{String.valueOf(i)},null,null
                    ,"_id DESC");
        }
        if(c.getCount() == 0){
            System.out.println("search:gender");
            c  = db.query("mail",null,"gender=?",new String[]{String.valueOf(i)},null,null
                    ,"_id DESC");
        }
        System.out.println(c.getCount());
        while(c.moveToNext()){
            long id = c.getLong(c.getColumnIndex("_id"));
            String name = c.getString(1);
            String number = c.getString(2);
            String grade = c.getString(3);
            String gender = c.getString(4);
            String email = c.getString(5);
            list.add(new Mail(id,name,number,grade,gender,email));
        }
        c.close();
        db.close();
        System.out.println(list);
        return list;
    }


    public List<Mail> queryAll(){
        SQLiteDatabase db = sqliteCon.getReadableDatabase();
        Cursor c = db.query("mail",null,null,null,null,null
        ,"_id DESC");
        List<Mail> list = new ArrayList<>();
        while(c.moveToNext()){
            long id = c.getLong(c.getColumnIndex("_id"));
            String name = c.getString(1);
            String number = c.getString(2);
            String grade = c.getString(3);
            String gender = c.getString(4);
            String email = c.getString(5);
            list.add(new Mail(id,name,number,grade,gender,email));
        }
        c.close();
        db.close();
        return list;
    }

}
